package com.supinfo.suptracking.dao.jpa;

import com.supinfo.suptracking.dao.CarDao;
import com.supinfo.suptracking.entity.Car;
import com.supinfo.suptracking.entity.Car_;
import com.supinfo.suptracking.entity.User;
import com.supinfo.suptracking.entity.Zone;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@Stateless
public class JpaCarDao implements CarDao{

    @PersistenceContext
    private EntityManager em;
    
    @Override
    public void addCar(Car car) {
        em.persist(car);
    }

    @Override
    public void delCar(Car car) { 
        em.remove(em.merge(car));
    }

    @Override
    public void delCar(Long id) {
        em.remove(id);
    }

    @Override
    public void updateCar(Car car) {
        em.merge(car);
    }

    @Override
    public List<Car> getAllCar() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Car> query = cb.createQuery(Car.class);
        return em.createQuery(query).getResultList();
    }

    @Override
    public List<Car> getCarByBrand(String brand) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Car> query = cb.createQuery(Car.class);
        Root<Car> car = query.from(Car.class);
        
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(car.get(Car_.brand), brand));
        
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        return em.createQuery(query).getResultList();
    }

    @Override
    public List<Car> getCarByInitialEntryIntoService(Date date) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Car> query = cb.createQuery(Car.class);
        Root<Car> car = query.from(Car.class);
        
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(car.get(Car_.initialEntryIntoService), date));
        
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        return em.createQuery(query).getResultList();
    }
    
    @Override
    public List<Car> getAllCarByUser(User user) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Car> query = cb.createQuery(Car.class);
        
        Root<Car> car = query.from(Car.class);
        List<Predicate> predicates = new ArrayList<>();
        
        predicates.add(cb.equal(car.get(Car_.user), user));
        
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        
        return em.createQuery(query).getResultList();
    }

    @Override
    public Car getCarByZone(Zone zone) {
        Car car = zone.getCar();
        return em.find(Car.class, car);
    }
    
    @Override 
    public Car getCarById(Long id) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Car> query = cb.createQuery(Car.class);
        Root<Car> invoice = query.from(Car.class);
        
        List<Predicate> predicates = new ArrayList<>();
        predicates.add(cb.equal(invoice.get(Car_.id), id));
        
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        
        if (!em.createQuery(query).getResultList().isEmpty())
        {
            return em.createQuery(query).getResultList().get(0);
        }
        else
        {
            Car car = null;
            return car;
        }
    }
}
